#include "PreCompile.h"
#include "Framework.h"

REGISTER_SINGLETON_MODULE(IFramework,Framework)

Framework* Framework::m_pInstance = NULL;

std::mutex* Framework::m_InstanceLock = new std::mutex;

// Get instance
Framework* Framework::GetInstance()
{
	if (m_pInstance == NULL)
	{
		std::lock_guard<std::mutex> Locker(*m_InstanceLock);

		if (m_pInstance == NULL)
		{
			m_pInstance = new Framework();
		}
	}

	return m_pInstance;
}

// Destory the instance
void Framework::DestoryInstance()
{
	if (m_InstanceLock)
	{
		delete m_InstanceLock;

		m_InstanceLock = NULL;
	}

	if (m_pInstance)
	{
		delete m_pInstance;

		m_pInstance = NULL;
	}
}

// Construct the Framework
Framework::Framework():
	m_pCore(NULL),
	m_bDisposed(false)
{
	Initialize();
}

// Detructe the Framework
Framework::~Framework()
{
	Destory();
}

// Initialize the Framework
void Framework::Initialize()
{
	// Create micro core
	CreateMicroCore();
}

// Destory the Framework
void Framework::Destory()
{
	if (!GetDisposed())
	{
		SetDisposed(true);

		// Destory micro core
		DestoryMicroCore();
	}
}

// Create micro core
void Framework::CreateMicroCore()
{
	m_CoreLoader.Load(MICRO_CORE_NAME);

	IMicroCore* pCore = m_CoreLoader.Data();

	SetMicroCore(pCore);
}

// Destory micro core
void Framework::DestoryMicroCore()
{
	
}

// Start the framework
bool Framework::Start()
{
	// Start the micro core
	if (!StartMicroCore())
	{
		return false;
	}

	return true;
}

// Stop the framework
bool Framework::Stop()
{
	// Stop the micro core
	StopMicroCore();

	return true;
}

// Start micro core
bool Framework::StartMicroCore()
{
	if (GetMicroCore() == NULL)
	{
		return false;
	}

	LOG_INFORMATION_EX("*************Start MicroCore****************");

	if (!GetMicroCore()->Start())
	{
		return false;
	}

	return true;
}

// Stop the micro core
bool Framework::StopMicroCore()
{
	if (GetMicroCore() == NULL)
	{
		return false;
	}

	if (!GetMicroCore()->Stop())
	{
		return false;
	}

	LOG_INFORMATION_EX("*************Stop MicroCore****************");

	return true;
}

// Is core's debug mode
bool Framework::IsDebugMode()
{
	if (GetMicroCore() == NULL)
	{
		return true;
	}

	return GetMicroCore()->IsDebugMode();
}

// Get the plugin by name and version
IPlugin* Framework::GetPlugin(std::string strPluginName,
	int iMajorVersion,
	int iMinorVersion,
	int iModifyVersion)
{
	if (GetMicroCore())
	{
		return GetMicroCore()->GetPlugin(strPluginName,
			iMajorVersion,
			iMinorVersion,
			iModifyVersion);
	}

	return NULL;
}

// Add Task to the micro core
bool Framework::AddTask(Task& UserTask)
{
	if (GetMicroCore())
	{
		return GetMicroCore()->AddTask(UserTask);
	}

	return false;
}

// Pause the task
void Framework::PauseTask(std::string strTaskId)
{
	if (GetMicroCore())
	{
		return GetMicroCore()->PauseTask(strTaskId);
	}
}

// Resume the task
void Framework::ResumeTask(std::string strTaskId)
{
	if (GetMicroCore())
	{
		return GetMicroCore()->ResumeTask(strTaskId);
	}
}

// Stop the task
void Framework::StopTask(std::string strTaskId)
{
	if (GetMicroCore())
	{
		return GetMicroCore()->StopTask(strTaskId);
	}
}
